package dbAccess;

import java.sql.*;
import java.util.ArrayList;

import exceptions.*;

import model.*;

public class MembreComiteDBAccess {
	public ArrayList<MembreComite> getAllMembreComite() throws PasIdentificationException, DatabaseErrorException{
		ArrayList<MembreComite> tabMemb = new ArrayList<MembreComite>();
		EtudiantDBAccess edba = new EtudiantDBAccess();
		Connection conn = DBConnexionSingleton.getInstance();
		PreparedStatement stat;
		String query = "select * from membre_comite;";
		try {
			stat = conn.prepareStatement(query);
			ResultSet data = stat.executeQuery();
			while(data.next()){
				MembreComite memb = new MembreComite(edba.getEtudiant(data.getInt("etudiant_id")), data.getString("password"), data.getString("fonction"));
				tabMemb.add(memb);
			}
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return tabMemb;
	}
	public MembreComite getMembreComite(int etudiant_id) throws PasIdentificationException, DatabaseErrorException{
		EtudiantDBAccess edba = new EtudiantDBAccess();
		Connection conn = DBConnexionSingleton.getInstance();
		PreparedStatement stat;
		MembreComite memb = null;
		String query = "select * from membre_comite where etudiant_id = ?;";
		try {
			stat = conn.prepareStatement(query);
				stat.setInt(1, etudiant_id);
			ResultSet data = stat.executeQuery();
			while(data.next()){
				memb = new MembreComite(edba.getEtudiant(etudiant_id), data.getString("password"), data.getString("fonction"));
			}
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return memb;
	}
	public void addMembreComite(MembreComite memb) throws PasIdentificationException, DatabaseErrorException{
		Connection conn = DBConnexionSingleton.getInstance();
		PreparedStatement stat;
		String query;
		try {
			query = "insert into membre_comite (etudiant_id,password,fonction) values (?,?,?);";
			stat = conn.prepareStatement(query);
				stat.setInt(1, memb.getId());
				stat.setString(2, memb.getPassword());
				stat.setString(3, memb.getFonction());
			stat.executeUpdate();
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
	}
	public MembreComite identification(String userLogin, String userPass) throws PasIdentificationException, BadPasswordException, DatabaseErrorException, BadLoginException{
		Connection conn = DBConnexionSingleton.getInstance();
		EtudiantDBAccess edba = new EtudiantDBAccess();
		Etudiant etu;
		MembreComite memb = null;
		PreparedStatement stat;
		String query;
		ResultSet data;
		try {
			etu = edba.getEtudiant(userLogin);
			
			query = "select * from membre_comite where etudiant_id = ?;";
			stat = conn.prepareStatement(query);
				stat.setInt(1, etu.getId());
			data = stat.executeQuery();
			while(data.next()){
				memb = new MembreComite(etu, data.getString("password"), data.getString("fonction"));
			}
			stat.close();
			
			if(memb==null || !(memb.getPassword().equals(userPass))) throw new BadPasswordException();
			else return memb;
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		} catch (NullPointerException ex){
			throw new BadLoginException();
		}
	}
}
